<?php 
	class Billing extends CI_Controller
		{
			
				function __construct()
					{
						parent::__construct();
						if(!$this->ion_auth->logged_in())
							{
								redirect('auth/login');
							}
						$this->load->model('user/invoice_model');
						$this->load->model('user/display_invoice_model');
						$this->load->model('user/billing_table_update_model','insert_into_table_model');
						$this->load->model('user/client_bill_model');
						$this->load->model('user/pdf_model');
						$this->load->model('user/task_model');
						$this->load->model('user/user_model');
						$this->load->model('user/about_model');
						$this->load->model('user/attendance_model');
						$this->load->model('user/task_model');
						$this->load->helper('url');
						$this->load->library('fpdf');
					}

				function display_invoice()
					{
						if(isset($id))
							{
								$user= $id;
							}
						else
							{
								$user=$this->session->userdata('user_id');		
							}
						$box = $this->input->post('box');
						$project_id = $this->input->post('project_id');
						if(!$this->input->post('box') && !$this->input->post('project_id'))
							{
								exit('No direct script access allowed');
							}
						$data['userid'] = $user;
						$page = $this->session->flashdata('page');
						$meta['page_title'] = "welcome user";
						$meta['message'] = "Logged In Successfully";	
						$data['box'] = $box;
						$data['client_address'] = $this->display_invoice_model->invoice_export_client_address($project_id);
						$data['task_details'] = $this->display_invoice_model->invoice_export_task_static_details($box);
						$data['rates_custom'] = $this->display_invoice_model->invoice_export_task_custom_rates($project_id,$box);
						$data['tax_recent'] = $this->display_invoice_model->invoice_export_tax_custom();
						$data['invoice_number'] = $this->display_invoice_model->invoice_number();
						$data['time_left'] = $this->display_invoice_model->invoice_export_time_dynamic($project_id,$box);
						$data['project_id'] = $project_id;
						$this->load->view('common/header', $meta);
						$this->load->view('user/display_invoice_view',$data);
						$this->load->view('common/footer');
					}
				
				function insert_into_table()
					{
						$project_id = $this->input->post('project_id');						
						$invoice_number = $this->input->post('invoice_number');
						$invoice_date = $this->input->post('invoice_date');
						$items_rate = $this->input->post('items_rate');
						$tax_amount = $this->input->post('tax_amount');
						$subtotal = $this->input->post('subtotal');
						$invoice_date = $this->input->post('invoice_date');
						$task_id = $this->input->post('task_id');
						$items_qty = $this->input->post('items_qty');
						$data['empty1'] = $this->insert_into_table_model->insert_into_invoice_details($invoice_number,$tax_amount,$subtotal,$invoice_date);
						$data['empty2'] = $this->insert_into_table_model->insert_into_invoice_info($project_id,$task_id,$items_qty,$items_rate,$invoice_number);
							
					}
					
				function pdf_generate()
					{
						$data['kind'] = $this->input->post('kind');
						$data['kind'] = $this->input->post('kind');
						$data['company_name'] = $this->input->post('company_name');
						$data['company_info'] = $this->input->post('company_info');
						$data['recipient_info'] = $this->input->post('recipient_info');
						$data['invoice_number_label'] = $this->input->post('invoice_number_label');
						$data['invoice_number'] = $this->input->post('invoice_number');
						$data['invoice_date_label'] = $this->input->post('invoice_date_label');
						$data['invoice_date'] = $this->input->post('invoice_date');
						$data['items_descp'] = $this->input->post('items_descp');
						$data['items_rate'] = $this->input->post('items_rate');
						$data['items_qty'] = $this->input->post('items_qty');
						$data['items_prc'] = $this->input->post('items_prc');
						$data['tax_percentage'] = $this->input->post('tax_percentage');
						$data['subtotal_label'] = $this->input->post('subtotal_label');
						$data['subtotal'] = $this->input->post('subtotal');
						$data['tax_name'] = $this->input->post('tax_name');
						$data['tax_amount'] = $this->input->post('tax_amount');
						$data['total_label'] = $this->input->post('total_label');
						$data['currency_symbol'] = $this->input->post('currency_symbol');
						$data['total'] = $this->input->post('total');
						$data['notes_a'] = $this->input->post('notes_a');
						$data['notes_b'] = $this->input->post('notes_b');
						define ('FPDF_FONTPATH', APPPATH. 'font/'); 
						$this->load->view('user/pdf_view',$data);
						redirect('/user/tasks', 'refresh');
					}	
					
				function client_bill($client_id = NULL)
					{
						$meta['page_title'] = "Project Billing";
						$meta['message'] = "Logged In Successfully";
						$data['result'] = $this->client_bill_model->populate_client_invoice($client_id);
						$data['project'] = $this->client_bill_model->populate_project_id_by_client_id($client_id);
						$data['cycle_by_client_id'] = $this->client_bill_model->populate_billing_cycle_by_client_id($client_id);
						$this->load->view('common/header', $meta);
						$this->load->view('user/client_bill_view',$data);
						$this->load->view('common/footer');
					}

				function task_list($tmp)
					{
						if(isset($id))
							{
								$user= $id;
							}
						else
							{
								$user=$this->session->userdata('user_id');		
							}
						$data['userid'] = $user;
						$page = $this->session->flashdata('page');
						$meta['page_title'] = "Project Billing";
						$meta['message'] = "Logged In Successfully";
						if($tmp == NULL)
							{
								exit('No direct script access allowed');
							}
						$project_id = $tmp;
						$meta['page_title'] = "Task list details";		
						$data['projects'] = $this->task_model->get_project_name_by_id($project_id);	
						$data['milestones'] = $this->task_model->get_default_milestone();
						$data['taskgroups'] = $this->task_model->get_default_taskgroups();
						$data['custom_taskgroups'] = $this->task_model->get_custom_taskgroups($project_id);
						$data['query'] = $this->invoice_model->populate_data($project_id);
						$data['query1'] = $this->invoice_model->count_row();
						$data['query3'] = $this->client_bill_model->populate_client_bill($tmp);
						$data['query4'] = $this->client_bill_model->populate_total_time($tmp);
						$data['query2'] = $project_id;
						$data['billing_cycle_project'] = $this->client_bill_model->populate_billing_cycle_by_project_id($project_id);
						$this->load->view('common/header',$meta);
						$this->load->view('user/task_details_views',$data);
						$this->load->view('common/footer');
						
					}
					
				function total_bill($client_id = NULL)
					{
						if(isset($client_id))
						$data['user_id'] = $client_id;
						$meta['page_title'] = "Client Billing Details";
						$meta['message'] = "Logged In Successfully";
						$input = $this->input->post();
						$data['user_id'] = $input['user'];
						$data['users'] = $this->ion_auth->users()->result();
						$data['result'] = $this->client_bill_model->populate_client_invoice($data['user_id']);
						$data['project'] = $this->client_bill_model->populate_project_id_by_client_id($data['user_id']);
						$data['cycle_by_client_id'] = $this->client_bill_model->populate_billing_cycle_by_client_id($data['user_id']);
						$this->load->view('common/header', $meta);
						$this->load->view('user/total_invoice_view',$data);
						$this->load->view('common/footer');
						
					}	
				
				//Backup Function
				/*		
				function index($tmp = NULL)
					{
						if(isset($id))
							{
								$user= $id;
							}
						else
							{
								$user=$this->session->userdata('user_id');		
							}
						$data['userid'] = $user;
						$page = $this->session->flashdata('page');
						$meta['page_title'] = "Project Billing";
						$meta['message'] = "Logged In Successfully";
						if($tmp == NULL)
							{
								exit('No direct script access allowed');
							}
						$project_id = $tmp;
						$project_id_tmp = $project_id;
						$data['query'] = $this->invoice_model->populate_data($project_id);
						$data['query1'] = $this->invoice_model->count_row();
						$data['query2'] = $project_id;
						$this->load->view('common/header', $meta);
						$this->load->view('user/invoice_view',$data);
						$this->load->view('common/footer');	
					}	
				*/	
		}
?>		